<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Form_creator
{
    var $CI;
    private $tabla;
    public $field;
    public $type;
    public $value;
    public $code;
    public $style;
    public $num_radio;
    public $descriptions;

    function Form_creator(){
        $this->CI =& get_instance();
    }

    function inicializa($config = array()){
        $defaults = array(
                    'field' => "",
                    'type' => "",
                    'value' => "",
                    'code' => "",
                    'style' => "",
                    'num_radio' => "",
                    'descriptions' => ""
                    );
        foreach ($defaults as $key => $val){
            if (isset($config[$key])){
                $this->$key = $config[$key];
            }else{
                $this->$key = $val;
            }
        }
    }

    function pintaCampo($config = null){
        $this->inicializa($config);
        $columna = $this->getDescField($this->field);
        switch ($this->type) {
            case 'SEL':
                $cadena  = "<select name='".$columna['Field']."[]' id='".$columna['Field']."' ".$this->style.">";
                $cadena .= $this->code;
                $cadena .= '</select>';
                break;
            case 'PWD':
                $cadena = "<input type='password' size='".$this->_getSize($columna['Type'])."' maxlength='".$this->_getLength($columna['Type'])."' ";
                $cadena .= "name='".$columna['Field']."[]' id='".$columna['Field']."'/>";
                break;
            case 'PWD2':
                $cadena = "<input type='password' name='".$this->field."[]' id='".$this->field."'/>";
                break;            
            case 'HDN':
                $cadena = "<input type='hidden' name='".$this->field."[]' id='".$this->field."' value='".$this->value."'/>";
                break;
            case 'HDN2':
                $cadena = "<input type='hidden' name='".$this->field."' id='".$this->field."' value='".$this->value."'/>";
                break;
            case 'FIL':
                $cadena = "<input type='file' name='".$this->field."' id='".$this->field."' value='".$this->value."' ".$this->style."/>";
                break;
            case 'RAD':
                $cadena = "";
                for($x = 0; $x < $this->num_radio; $x++){
                    $cadena .= '<input type="radio" name="'.$this->field.'" id="'.$this->field.'"
                        value="'.$x.'" '.($this->value == $x ? "checked='checked'" : "").'>
                        <span id="spn_'.$this->field.'">'.$this->descriptions[$x]."</span>";
                }
                break;
            case 'FEC':
                $cadena = "<input type='text' class='datepicker' readonly='true' size='10'
                    name='".$this->field."[]' id='".$this->field."' value='".$this->value."'/>";
                break;            
            case 'TXT':
                $cadena = "<span name='".$this->field."[]' id='".$this->field."'>".$this->value."</span>";
                break;
            case 'CHK':
                $chequeado = "";
                if($this->value == 'S'){
                    $chequeado = "checked";
                }           
                $cadena = "<input type='checkbox' name='".$this->field."' id='".$this->field."' ".$chequeado."/>";
                break;
            default:
                if(is_array($columna)){
                    switch ($this->_getType($columna['Type'])) {
                        case 'varchar':
                            if($this->_getLength($columna['Type']) == 1){
                                $chequeado = "";
                                if($this->value == 'S'){
                                    $chequeado = "checked";
                                }
                                $cadena = "<input type='checkbox' name='".$columna['Field']."[]' id='".$columna['Field']."' ".$chequeado."/>";
                            }else if($this->_getLength($columna['Type']) > 249){
                                $cadena = "<textarea name='".$columna['Field']."[]' id='".$columna['Field']."'>".$this->value."</textarea>";
                            }else{
                                $cadena  = "<input type='text' size='".$this->_getSize($columna['Type'])."' maxlength='".$this->_getLength($columna['Type'])."' ";
                                $cadena .= "name='".$columna['Field']."[]' id='".$columna['Field']."' value='".$this->value."'/>";
                            }
                            break;
                        case 'int':
                            $cadena = "<input type='text' maxlength='".$this->_getLength($columna['Type'])."'
                                name='".$columna['Field']."[]' id='".$columna['Field']."' value='".$this->value."' onkeypress='soloNum(event)'/>";
                            break;
                        case 'binary':
                            $chequeado = "";
                            if($this->value == 'S'){
                                $chequeado = "checked";
                            }                            
                            $cadena = "<input type='checkbox' name='".$columna['Field']."[]' id='".$columna['Field']."' ".$chequeado."/>";
                            break;                        
                        case 'date' || 'timestamp':
                            $cadena = "<input type='text' class='date-pick dp-applied' readonly='true' size='8'
                                name='".$columna['Field']."[]' id='".$columna['Field']."' value='".$this->value."'/>";
                            break;
                    }
                }else{
                    if($columna == ''){
                        $cadena = "<input type='text' name='".$this->field."' id='".$this->field."' value='".$this->value."' ".$this->style."/>";                    
                    }else{
                        $cadena = "<input type='text' name='".$columna."' id='".$columna."' value='".$this->value."'/>";                    
                    }
                }
        }
        return $cadena;
    }

    function getTabla(){
        return $this->tabla;
    }

    function setTabla($tabla){
        $this->tabla = $tabla;
    }

    function getDescField($field = ''){
        $query = $this->CI->db->query("DESCRIBE ".$this->getTabla());
        foreach ($query->result_array() as $columnas){
            if ($columnas['Field'] == $field) {
                return $columnas;
            }
        }
    }

    function _getType($cadena = ''){
        $pos = strpos($cadena, "(");
        if($pos == ''){
            $pos = strlen($cadena);
        }
        return substr($cadena, 0, $pos);
    }

    function _getLength($cadena = ''){
        $longitud = strlen($cadena);
        $posIni = strpos($cadena, "(");
        $posFin = stripos($cadena, ")");
        $posFin = ($longitud - $posFin);
        return substr($cadena, $posIni + 1, -$posFin);
    }

    function _getSize($cadena = ''){
        $longitud = strlen($cadena);
        $posIni = strpos($cadena, "(");
        $posFin = stripos($cadena, ")");
        $posFin = ($longitud - $posFin);
        $size = ((int)substr($cadena, $posIni + 1, -$posFin)/2) + 7;
        if($size > 37){
            $size = 45;
        }
        return $size;
    }

}
?>